<?php
/*
 * (C) Copyright 2013 Torsten Gorath (http://www.blauesdach.de/).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Contributors:
 *     Torsten Gorath
 */
require_once $basedir."/inc/mitarbeiter.php";
require_once $basedir."/inc/mitarbeiterAbwesend.php";
$mitarbeiter = new Mitarbeiter();
if(isset($_REQUEST['id']) && $_REQUEST['id']!="") {
    $mitarbeiter = new Mitarbeiter($_REQUEST['id']);
}
if(isset($_REQUEST['save'])) {
    $mitarbeiter->setAdresseId($_REQUEST['adresse_id']);
    $mitarbeiter->setRolle($_REQUEST['rolle']);
    $mitarbeiter->setGruppenId($_REQUEST['gruppen_id']);
    $mitarbeiter->setEingetrettenAm($_REQUEST['eingetretten_am']);
    $mitarbeiter->setAusgeschiedenAm($_REQUEST['ausgeschieden_am']);
    $mitarbeiter->setBilderDruckErlaubt(isset($_REQUEST['bilder_druck_erlaubt'])?1:0);
    $mitarbeiter->setBilderInternetErlaubt(isset($_REQUEST['bilder_internet_erlaubt'])?1:0);
    $mitarbeiter->save();
}
$gruppe = $mitarbeiter->getGruppe();
$adress = $mitarbeiter->getAdress();
?>
<script language="JavaScript" type="text/javascript">
    $(function() {
        $('#nurAktuelle').click(function(event) {
            var params = [
                'page=<?php echo $menuid; ?>',
                'onlyCurrent=' + $('#nurAktuelle').is(':checked')
            ];
            window.location.href = "http://" + window.location.host + window.location.pathname + "?" + params.join('&');
        });
        $("input[name=adresse]").autocomplete({
            source: function(request, response) {
                var data = {
                    'value': request.term,
                    'page': 'stammdaten_adress',
                    'action': 'search'
                };
                $.ajax({
                    'url': "json.php",
                    'data': data,
                    'type':'post',
                    'dataType': 'json',
                    'success': function(data) {
                        response(data.items);
                    }
                })
            },
            minLength: 2,
            select: function(event, ui) {
                var item = ui.item;
                $('input[name=adresse_id]').attr('value', item.id);
                $('#stammdaten_mitarbeiter_add_adress').html(item.strasse+" "+item.hausnummer+item.zusatz_hausnummer+"<br />"+item.plz+" "+item.ort)
            }
        })
        .data("ui-autocomplete")._renderItem = function(ul, item) {
            item.label = "<a>" + item.vorname + " " + item.nachname + "<br />"
                    + item.strasse + " " + item.hausnummer + item.zusatz_hausnummer + "<br />"
                    + item.plz + " " + item.ort + "</a>";
            item.value = item.vorname + " " + item.nachname;
            return $("<li></li>")
                .data("item.autocomplete", item)
                .append(item.label)
                .appendTo(ul);
        };
        /*
         * Funktionen fuer Krankmeldungen
         */
        $('#btnAddKrank').click(function() {
            $("#frmAbwesenheit").find("[name='anfang_am']").val('');
            $("#frmAbwesenheit").find("[name='ende_am']").val('');
            $("#frmAbwesenheit").find("[name='bemerkung']").val('');
            $("#frmAbwesenheit").find("[name='krankmeldung_ok']").attr('checked',false);
            $("#frmAbwesenheit").find("[name='reason'] option").each(function() {
                if($(this).val() == 'Unbekannt') {
                    $(this).attr('selected', 'selected');
                }
            });
            $("#frmAbwesenheit").find("[name='id']").val('');
            $('#frmAbwesenheit').css('display','block');
        });
        $('#btnAbortAdd').click(function() {
            $('#frmAbwesenheit').css('display','none');
        })
        $('#frmAbwesenheit').submit(function(event) {
            data = {
                "id":this.id.value,
                "action":this.action.value,
                "page":this.page.value,
                "mitarbeiter_id":this.mitarbeiter_id.value,
                "anfang_am":this.anfang_am.value,
                "ende_am":this.ende_am.value,
                "krankmeldung_ok":this.krankmeldung_ok.checked,
                "reason":this.reason.value,
                "bemerkung":this.bemerkung.value
            };
            $.post(
                "json.php",
                data,
                function(data,textStatus,jqXHR) {
                    $('#frmAbwesenheit').css('display','none');
                    if(textStatus=='success') {
                        $('#abwesenheiten').empty();
                        for(i=0;i<data.items.length;i++) {
                            value = data.items[i];
                            $('#abwesenheiten').append($('<tr/>')
                                .append('<td>'+value.reason+'</td>')
                                .append('<td>'+value.anfang_am+'</td>')
                                .append('<td>'+value.ende_am+'</td>')
                                .append('<td>'+value.krankmeldung_ok?'X':''+'</td>')
                                .append($('<td/>')
                                    .append($('<a name="edit_abwesend" href="'+value.id+'"/>')
                                        .text('edit')
                                        .click(function (event) {
                                            return edit_abwesend(event);
                                        }))));
                        }
                    } else {
                        alert(textStatus);
                    }
                },
                'json'
            );
            return false;
        })
        function edit_abwesend(event) {
            data = {
                "action":"get_abwesend",
                "id":$(event.target).attr('href'),
                "page":$('#page_id').val()
            };
            $.post(
                "json.php",
                data,
                function(data,textStatus,jqXHR) {
                    $("#frmAbwesenheit").find("[name='anfang_am']").val(data.anfang_am);
                    $("#frmAbwesenheit").find("[name='ende_am']").val(data.ende_am);
                    $("#frmAbwesenheit").find("[name='bemerkung']").val(data.bemerkung);
                    $("#frmAbwesenheit").find("[name='krankmeldung_ok']").attr('checked',data.krankmeldung_ok);
                    $("#frmAbwesenheit").find("[name='reason'] option").each(function() {
                        if($(this).val() == data.reason) {
                            $(this).attr('selected', 'selected');
                        }
                    });
                    $("#frmAbwesenheit").find("[name='id']").val(data.id);
                    $('#frmAbwesenheit').css('display','block');
                },
                'json'
            );
            return false;
        }
        $("[name='edit_abwesend']").click(function(event) {
            return edit_abwesend(event);
        });
    });
</script>
<?php
if($action=='edit') {
    $action = 'add';
}
if($action=='add') {
    $form_display = "";
    $abwesenheiten = MitarbeiterAbwesend::getAll($mitarbeiter->getId(),false);
    $reasons = MitarbeiterAbwesend::getReasons();
} else {
    $form_display = " style='display:none;'";
    $reasons = array();
    $abwesenheiten = array();
}
$gruppen = Gruppen::getAll();
?>
<div class='inline-block'<?php echo $form_display; ?>>
    <form id='stammdaten_mitarbeiter_add_form' method="post" action="/index.php">
    <input type="hidden" name="page" value="<?php echo $menuid;?>" />
    <input type="hidden" name="id" value="<?php echo $mitarbeiter->getId(); ?>" />
        <div>
            <div class='inline-block label'>Adresse:</div>
            <div class='inline-block'>
                <input name="adresse_id" type="hidden" value="<?php echo $mitarbeiter->getAdresseId(); ?>" />
                <input type="text" name="adresse" maxlength="101" value="<?php echo ($adress->getVorname()!=""?$adress->getVorname()." ":"").$adress->getNachname();?>" /><br />
                <span id="stammdaten_mitarbeiter_add_adress">
                    <?php echo $adress->getStrasse()." ".$adress->getHausnummer().$adress->getZusatzHausnummer(); ?><br />
                    <?php echo $adress->getPlz()." ".$adress->getOrt(); ?>
                </span>
            </div>
        </div>
        <div>
            <div class='inline-block label'>Rolle:</div>
            <div class='inline-block'>
                <select name='rolle'>
<?php 
foreach ($mitarbeiter->getRollen() as $value) {
    $selected = "";
    if($value == $mitarbeiter->getRolle()) {
        $selected = " selected";
    }
    echo "<option value='$value'$selected>$value</option>";
}
?>
                </select>
            </div>
        </div>
        <div>
            <div class='inline-block label'>Gruppe:</div>
            <div class='inline-block'>
               <select name='gruppen_id'>
                    <option value='none'></option>
<?php
        for($x=0;$x<sizeof($gruppen);$x++) {
            $selected = "";
            if($gruppen[$x]->getId() == $mitarbeiter->getGruppenId()) {
                $selected = "selected ";
            }
?>
                    <option value='<? echo $gruppen[$x]->getId(); ?>' <? echo $selected; ?>><? echo $gruppen[$x]->getName(); ?></option>
<?php
        }
?>
                </select>
            </div>
        </div>
        <div>
            <div class='inline-block label'>Eingetretten:</div>
            <div class='inline-block'>
                <input type="text" name="eingetretten_am" maxlength="10" value="<?php echo $mitarbeiter->getEingetettenAm(); ?>" /><br />
            </div>
        </div>
        <div>
            <div class='inline-block label'>Ausgeschieden:</div>
            <div class='inline-block'>
                <input type="text" name="ausgeschieden_am" maxlength="10" value="<?php echo $mitarbeiter->getAusgeschiedenAm(); ?>" /><br />
            </div>
        </div>
        <div>
            <div class='inline-block'><input name="bilder_druck_erlaubt" type="checkbox" value="true" <?php echo $mitarbeiter->isBilderDruckErlaubt()?"checked='true'":""; ?>" /> Erlaubnis f&uuml;r Bilder Druck:</div>
        </div>
        <div>
            <div class='inline-block'><input name="bilder_internet_erlaubt" type="checkbox" value="true" <?php echo $mitarbeiter->isBilderInternetErlaubt()?"checked='true'":""; ?>" /> Erlaubnis f&uuml;r Bilder Online:</div>
        </div>
        <div>
            <button type="submit" name="save">Speichern</button>
            <button type="submit" name="reset">Abbrechen</button>
        </div>
    </form>
</div>
<div class='inline-block'<?php echo $form_display; ?>>
    <button id='btnAddKrank'>Abwesenheit Hinzuf&uuml;gen</button>
    <table>
        <tbody id='abwesenheiten'>
<?php
foreach($abwesenheiten as $value) {
?>
            <tr>
                <td><? echo $value->getReason(); ?></td>
                <td><? echo $value->getAnfangAm(); ?></td>
                <td><? echo $value->getEndeAm(); ?></td>
                <td><? echo $value->isKrankmeldungOk()?'X':''; ?></td>
                <td><? echo $value->getBemerkung(); ?></td>
                <td><a name='edit_abwesend' href='<? echo $value->getId(); ?>'>edit</a></td>
            </tr>
<?php
}
?>
        </tbody>
    </table>
    <form id='frmAbwesenheit' style='display:none;'>
        <input type='hidden' name='id' value='' />
        <input type='hidden' name='mitarbeiter_id' value='<? echo $mitarbeiter->getId(); ?>'/>
        <input type='hidden' name='action' value='save_abwesend' />
        <input type='hidden' name='page' id='page_id' value='<? echo $menuid; ?>' />
        <div>
            <div class='inline-block label'>Grund:</div>
            <div class='inline-block'>
                <select name='reason'>
<?php foreach($reasons as $value) { ?>
                    <option value='<?php echo $value; ?>'><?php echo $value; ?></option>
<?php } ?>
                </select>
            </div>
        </div>
        <div>
            <div class='inline-block label'>Anfang:</div>
            <div class='inline-block'>
                <input type='date' name='anfang_am' maxlength="10" />
            </div>
        </div>
        <div>
            <div class='inline-block label'>Ende:</div>
            <div class='inline-block'>
                <input type='date' name='ende_am' maxlength="10" />
            </div>
        </div>
        <div>
            <input type='checkbox' name='krankmeldung_ok' />
            Meldung schriftlich erhalten, oder nicht notwendig
        </div>
        <div>
            <div class='inline-block label'>Bemerkung:</div>
            <div class='inline-block'>
                <textarea name='bemerkung'></textarea>
            </div>
        </div>
        <div>
            <button type='submit' id='btnSaveAdd'>Speichern</button>
            <button type='reset' id='btnAbortAdd'>Abbrechen</button>
        </div>
    </form>
</div>
<?php
if($action!="add") {
    $onlyCurrent=true;
    if(isset($_REQUEST['onlyCurrent'])) {
        $onlyCurrent = $_REQUEST['onlyCurrent']=='true'?true:false;
    }
    $mitarbeiters = Mitarbeiter::getAll($onlyCurrent);
?>
<div>
    <a href='?page=stammdaten_mitarbeiter&action=add'>Neuer MitarbeiterIn</a> ||
    <input type='checkbox' value='' name='nurAktuelle' id='nurAktuelle' <?php echo $onlyCurrent?'checked':''; ?>></input> Nur Aktuelle
</div>
<div>
    <table border="1" bordercolor="black" cellpadding="0" cellspacing="0" >
        <tr>
            <th></th>
            <th>Name</th>
            <th>Rolle</th>
            <th>Gruppe</th>
            <th>Eingetretten</th>
            <th></th>
        </tr>
<?php
if($mitarbeiters!=NULL) {
    for($i=0;$i<count($mitarbeiters);$i++) {
        $value = $mitarbeiters[$i];
        $adress = $value->getAdress();
        $css_class = "even";
        if($i%2==1) {
            $css_class = "odd";
        }
?>
        <tr class="<?php echo $css_class; ?>">
            <td><?php echo ($i+1); ?></td>
            <td><?php echo $adress->getVorname()." ".$adress->getNachname();?></td>
            <td><?php echo $value->getRolle();?></td>
            <td><?php echo $value->getGruppe()->getName();?></td>
            <td><?php echo $value->getEingetettenAm(); ?></td>
            <td><a href="?page=<?php echo $menuid; ?>&action=edit&id=<?php echo $value->getId();?>">edit</a></td>
        </tr>
<?php
    }
}
?>
    </table>
</div>
<?php    
}
?>